package com.zxw.mallafter.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxw.mallafter.dao.AdminDao;
import com.zxw.mallafter.model.AdminExtend;
import com.zxw.mallafter.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.PageInfo;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao;

    @Autowired
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    public List<AdminExtend> getall(PageInfo pageInfo,AdminExtend adminExtend) {
        //开启分页
        Page<?> __= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        return adminDao.getall(adminExtend);
    }

    @Override
    public int delete(Integer[] ids) {
        try {
            return adminDao.delete(ids);
        }catch(Exception e){
            return 0;
        }

    }

    @Override
    public int insert(AdminExtend adminExtend) {
        // 判断 id，admin_id，admin_name，sex，password，birthday，role 是否为空或空字符串
        if (adminExtend.getAdminId() == null || adminExtend.getAdminName() == null ||
                adminExtend.getSex() == null || adminExtend.getPassword() == null || adminExtend.getBirthday() == null
                || adminExtend.getRole() == null || adminExtend.getAdminId().isEmpty() || adminExtend.getAdminName().isEmpty() ||
                adminExtend.getSex().isEmpty() || adminExtend.getPassword().isEmpty() ||
                adminExtend.getRole().isEmpty()) {
            return 0;
        }
        try {
            return adminDao.insert(adminExtend);
        }catch(Exception  e){
            return 0;
        }
    }

    @Override
    public int update(AdminExtend adminExtend) {
        // 判断 id，admin_id，admin_name，sex，password，birthday，role 是否为空或空字符串
        if (adminExtend.getAdminId() == null || adminExtend.getAdminName() == null ||
                adminExtend.getSex() == null || adminExtend.getPassword() == null || adminExtend.getBirthday() == null
                || adminExtend.getRole() == null || adminExtend.getAdminId().isEmpty() || adminExtend.getAdminName().isEmpty() ||
                adminExtend.getSex().isEmpty() || adminExtend.getPassword().isEmpty() ||
                adminExtend.getRole().isEmpty()) {
            return 0;
        }
       try{
           return adminDao.update(adminExtend);
       }catch(Exception e){
           return 0;
        }
    }
}
